package com.shsxt.service;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shsxt.controller.YunTongXun;
import com.shsxt.dao.AuntDao;
import com.shsxt.dao.IUserinfoDao;
import com.shsxt.dao.OrderDao;
import com.shsxt.dao.WodeDao;
import com.shsxt.dao.YuyueDao;
import com.shsxt.model.COrder;
import com.shsxt.model.DOrder;
import com.shsxt.model.Income;
import com.shsxt.model.JOrder;
import com.shsxt.model.KOrder;
import com.shsxt.model.Notified;
import com.shsxt.model.ROrder;
import com.shsxt.model.SOrder;
import com.shsxt.model.ServicePersonal;
import com.shsxt.model.WithdrawDetail;
import com.shsxt.vo.AuntOrderQuery;
import com.shsxt.vo.DayTime;
import com.shsxt.vo.Evaluation;
import com.shsxt.vo.OrderCircle;
import com.shsxt.vo.OrderQuery;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;

@Service
public class AuntService {
	@Autowired
	private IUserinfoDao dao;
	@Autowired
	private WodeDao wodeDao;
	@Autowired
	private WodeService wodeService;
	@Autowired
	private AuntDao auntDao;
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private YuyueDao yuyueDao;
	//订单所有(该用户所有订单，共3块)
	public Map<String, Object> all(String servicePersonal) throws ParseException {
		List<AuntOrderQuery> list=auntDao.findAuntOrder(servicePersonal);
		List<AuntOrderQuery> listall=new ArrayList<AuntOrderQuery>();
		SimpleDateFormat sp=new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat shifen=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		AuntOrderQuery oq=null;
		String name="";
		for(AuntOrderQuery aq:list){
			name=wodeDao.findUserByPhone(aq.getPhoneNumber()).getPetName();
			if(aq.getType().equals("日常保洁")){
				System.err.println("测试"+aq.getCleaningSupplies_Price());
				oq=new AuntOrderQuery(aq.getOrderNumber(),aq.getType(),aq.getOrderTime(),aq.getAddress()+aq.getDetailAddress(),name,aq.getOrderState(),aq.getTimeSlot()+"("+aq.getServiceTime()+"小时)",aq.getService_Price(),"",aq.getRealPhoneNumber(),shifen.format(sp.parse(aq.getOrderNumber())),aq.getLocation(),aq.getCleaningSupplies_Price()+"");
			}else{
				oq=new AuntOrderQuery(aq.getOrderNumber(),aq.getType(),aq.getOrderTime()+" "+aq.getTimeSlot(),aq.getAddress()+aq.getDetailAddress(),name,aq.getOrderState(),aq.getServiceTime(),aq.getService_Price(),"",aq.getRealPhoneNumber(),shifen.format(sp.parse(aq.getOrderNumber())),aq.getLocation(),"");
			}
			listall.add(oq);
		}
		
		

		//周期订单集合
		Map<String,Object> param1=new HashMap<String,Object>();
		param1.put("servicePersonal", servicePersonal);
		param1.put("phoneNumber", "");
		List<OrderCircle> ocs=orderDao.findDoneScoreCircle(param1);
		ocs.addAll(orderDao.findDoneUnscoreCircle(param1));
		ocs.addAll(orderDao.findWorkingCircle(param1));
		
		//遍历(已完成或待评价的面试订单集合)加入到已完成里面
		for(OrderCircle oc:ocs){
			name=wodeDao.findUserByPhone(oc.getPhoneNumber()).getPetName();
			String[] days=oc.getCircle().split(";");
			String[] day=days[0].split(",");
			String[] slot=days[1].split(",");
			SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
			SimpleDateFormat to2=new SimpleDateFormat("EEEE");
			SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
			Date date=from.parse(day[0]);
			String da=to.format(date);
			String da2=to2.format(date);
			if(days[2].equals("一周一次")){
				//					orderNumber 				type  				orderTime 			address 	detailAddress 			orderState	 	timeSlot 		service_Price		serviceTime

				oq=new AuntOrderQuery(oc.getOrderNumber(),"周期订单(一周一次)",da,oc.getAddress()+oc.getDetailAddress(),name,oc.getOrderState(),da2+slot[0]+"("+oc.getServiceTime()+"小时)",oc.getService_price(),days[3]+"周",oc.getRealPhoneNumber(),shifen.format(sp.parse(oc.getOrderNumber())),oc.getLocation(),oc.getCleaningSupplies_Price());
					listall.add(oq);
			}else if(days[2].equals("两周一次")){
				oq=new AuntOrderQuery(oc.getOrderNumber(),"周期订单(两周一次)",da,oc.getAddress()+oc.getDetailAddress(),name,oc.getOrderState(),da2+slot[0]+"("+oc.getServiceTime()+"小时)",oc.getService_price(),Integer.valueOf(days[3])+"次",oc.getRealPhoneNumber(),shifen.format(sp.parse(oc.getOrderNumber())),oc.getLocation(),oc.getCleaningSupplies_Price());
					listall.add(oq);
			}else if(days[2].equals("一周多次")){
				String times="";
				int a=day.length/Integer.valueOf(days[3]);
				for(int x=0;x<a;x++){
					String str=to2.format(from.parse(day[x]));
					times=times+str+slot[x]+"("+getTimeCount(slot[x])+"小时);";
					
				}
				//						orderNumber 				type  				orderTime 			address 	detailAddress 			orderState	 	timeSlot 		service_Price		serviceTime
				oq=new AuntOrderQuery(oc.getOrderNumber(),"周期订单(一周多次)",da,oc.getAddress()+oc.getDetailAddress(),name,oc.getOrderState(),times,oc.getService_price(),days[3]+"周",oc.getRealPhoneNumber(),shifen.format(sp.parse(oc.getOrderNumber())),oc.getLocation(),oc.getCleaningSupplies_Price());
					listall.add(oq);
			}
		}
		
			   
		
		
		
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("list",paixuAuntOrderQuery(listall));
		return param;
	}
	
	//点击服务完成   更新订单状态
	public  synchronized  Map<String, Object> state(String orderNumber, String type) throws ParseException, APIConnectionException, APIRequestException {
		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, Object> param = new HashMap<String,Object>();
		int l=-1;
		if(type.equals("日常保洁")||type.equals("周期订单(一周一次)")||type.equals("周期订单(两周一次)")||type.equals("周期订单(一周多次)")){
			 ROrder r= orderDao.findROrder(orderNumber);
			 if(!r.getOrderState().equals("已接单")){
				 map.put("0", "失败");
				 return map;
			 }
			
			/*if(r.getOrderTime()!=null&&new Date().getTime()< new SimpleDateFormat("yyyy-MM-dd").parse( r.getOrderTime()).getTime()){
				 map.put("0", "失败");
				 return map;
			}else if(r.getOrderTime()==null&&new Date().getTime()< new SimpleDateFormat("yyyy年MM月dd日").parse( r.getCircle().split(";")[0].split(",")[r.getCircle().split(";")[0].split(",").length-1]).getTime()){
				 map.put("0", "失败");
				 return map;
			}*/
			 l=auntDao.updateRState( orderNumber);

			param.put("type", type);
			param.put("aunt", r.getServicePersonal());
			param.put("count", "+"+r.getService_Price());
			//创建余额明细记录
			auntDao.createIncome(param);
			//给用户发送短信
			/*stateNotified( r.getPhoneNumber(), type, orderNumber);*/
			new YunTongXun().register(r.getPhoneNumber(),"86615",null);
			//增加阿姨余额   服务次数+1
			param.put("count", r.getService_Price());

			auntDao.updateAuntBalance(param);
		}else if(type.equals("沙发护理")){
			 SOrder r= orderDao.findSOrder(orderNumber);
			 if(!r.getOrderState().equals("已接单")){
				 map.put("0", "失败");
				 return map;

			 }
			/* if(new Date().getTime()< new SimpleDateFormat("yyyy-MM-dd").parse( r.getOrderTime()).getTime()){
				 map.put("0", "失败");
				 return map;
			}*/
			 l=auntDao.updateSState( orderNumber);

				param.put("type", type);
				param.put("aunt", r.getServicePersonal());
				param.put("count", "+"+r.getService_Price());
				//创建余额明细记录
				auntDao.createIncome(param);
				//给用户发送通知
				stateNotified( r.getPhoneNumber(), type, orderNumber);
				//增加阿姨余额
				param.put("count", r.getService_Price());
				auntDao.updateAuntBalance(param);
		}else if(type.equals("开荒保洁")){
			 KOrder r= orderDao.findKOrder(orderNumber);
			 if(!r.getOrderState().equals("已接单")){
				 map.put("0", "失败");
				 return map;

			 }
			/* if(new Date().getTime()< new SimpleDateFormat("yyyy-MM-dd").parse( r.getOrderTime()).getTime()){
				 map.put("0", "失败");
				 return map;
			}*/
			 l=auntDao.updateKState( orderNumber);

				param.put("type", type);
				param.put("aunt", r.getServicePersonal());
				param.put("count", "+"+r.getService_Price());
				//创建余额明细记录
				auntDao.createIncome(param);
				//给用户发送通知
				stateNotified( r.getPhoneNumber(), type, orderNumber);
				//增加阿姨余额
				param.put("count", r.getService_Price());
				auntDao.updateAuntBalance(param);
		}else if(type.equals("家电清洗")){
			 JOrder r= orderDao.findJOrder(orderNumber);
			 if(!r.getOrderState().equals("已接单")){
				 map.put("0", "失败");
				 return map;

			 }
			 /*if(new Date().getTime()< new SimpleDateFormat("yyyy-MM-dd").parse( r.getOrderTime()).getTime()){
				 map.put("0", "失败");
				 return map;
			}*/
			 l=auntDao.updateJState( orderNumber);

				param.put("type", type);
				param.put("aunt", r.getServicePersonal());
				param.put("count", "+"+r.getService_Price());
				
				//创建余额明细记录
				auntDao.createIncome(param);
				//给用户发送通知
				stateNotified( r.getPhoneNumber(), type, orderNumber);
				//增加阿姨余额
				param.put("count", r.getService_Price());
				auntDao.updateAuntBalance(param);
		}else if(type.equals("地板护理")){
			 DOrder r= orderDao.findDOrder(orderNumber);
			 if(!r.getOrderState().equals("已接单")){
				 map.put("0", "失败");
				 return map;

			 }
			/* if(new Date().getTime()< new SimpleDateFormat("yyyy-MM-dd").parse( r.getOrderTime()).getTime()){
				 map.put("0", "失败");
				 return map;
			}*/
			 l=auntDao.updateDState( orderNumber);

				param.put("type", type);
				param.put("aunt", r.getServicePersonal());
				param.put("count", "+"+r.getService_Price());
				//创建余额明细记录
				auntDao.createIncome(param);
				//给用户发送通知
				stateNotified( r.getPhoneNumber(), type, orderNumber);
				//增加阿姨余额
				param.put("count", r.getService_Price());
				auntDao.updateAuntBalance(param);
		}else if(type.equals("擦玻璃")){
			 COrder r= orderDao.findCOrder(orderNumber);
			 if(!r.getOrderState().equals("已接单")){
				 map.put("0", "失败");
				 return map;

			 }
			/* if(new Date().getTime()< new SimpleDateFormat("yyyy-MM-dd").parse( r.getOrderTime()).getTime()){
				 map.put("0", "失败");
				 return map;
			}*/
			 l=auntDao.updateCState(orderNumber);

				param.put("type", type);
				param.put("aunt", r.getServicePersonal());
				param.put("count", "+"+r.getService_Price());
				//创建余额明细记录
				auntDao.createIncome(param);
				//给用户发送通知
				stateNotified( r.getPhoneNumber(), type, orderNumber);
				//增加阿姨余额
				param.put("count", r.getService_Price());
				auntDao.updateAuntBalance(param);
		}
		if(l!=-1){
			map.put("0", "成功");
		}else{
			map.put("0", "失败");
		}
		
		return map;

	}

	public Map<String, Object> evaluation(String servicePersonal) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		List<Evaluation> evaluations=new ArrayList<Evaluation>();
		evaluations=auntDao.evaluation(servicePersonal);
		map.put("evaluations", paixuEvaluation(evaluations));
		return map;
	}
	public Map<String, Object> oneEvaluation(String orderNumber,String type) {
		Map<String, Object> map = new HashMap<String,Object>();
		Evaluation evaluation=new Evaluation();
		
		if(type.equals("日常保洁")||type.equals("周期订单(一周一次)")||type.equals("周期订单(一周多次)")||type.equals("周期订单(两周一次)")){
			evaluation=auntDao.REvaluation( orderNumber);
		}else if(type.equals("沙发护理")){
			evaluation=auntDao.SEvaluation( orderNumber);
		}else if(type.equals("开荒保洁")){
			evaluation=auntDao.KEvaluation( orderNumber);
		}else if(type.equals("家电清洗")){
			evaluation=auntDao.JEvaluation( orderNumber);
		}else if(type.equals("地板护理")){
			evaluation=auntDao.DEvaluation( orderNumber);
		}else if(type.equals("擦玻璃")){
			evaluation=auntDao.CEvaluation( orderNumber);
		}
		map.put("evaluation", evaluation);
		return map;
	}
	public Map<String, Object> account(String servicePersonal) throws ParseException {
		ServicePersonal sp= auntDao.getAuntInformationByName(servicePersonal);
		Map<String,Object> map=new HashMap<String,Object>();
		List<Income> incomes=new ArrayList<Income>();
		incomes=auntDao.findIncomes(servicePersonal);
		map.put("balance", sp.getBalance()+"");
		map.put("detail", paixuIncome(incomes));
		return map;
	}
	//消息通知

	public Map<String, Object> notified(String servicePersonal,String type) throws ParseException {
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("phoneNumber", servicePersonal);
		param.put("type", type);

		List<Notified> notifies=new ArrayList<Notified>();
		notifies=auntDao.getNotified(param);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("notifies", paixuNotified(notifies));
		return map;
	}

	public Map<String, Object> withdrawDetail(String servicePersonal) throws ParseException {
		List<WithdrawDetail> withdrawDetails=new ArrayList<WithdrawDetail>();
		withdrawDetails=auntDao.findWithdrawDetailByName(servicePersonal);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("withdrawDetails", paixuWithdrawDetail(withdrawDetails));
		return map;
	}
	//提现提交
	public  synchronized  Map<String, Object> withdraw(String servicePersonal, String type,String count, String number, String check) throws ParseException {
		Map<String,Object> duanxin=new HashMap<String,Object>();
		duanxin=wodeService.duanxin(servicePersonal,"阿姨提现",check);
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("servicePersonal", servicePersonal);
		param.put("count", "+"+count);
		param.put("number", number);
		param.put("type", type);
		param.put("phoneNumber", servicePersonal);
		param.put("balance", count);

		Map<String,Object> map=new HashMap<String,Object>();
		if(duanxin.get("0").equals("成功")){
			auntDao.addWithdraw(param);
			auntDao.updateBalanceBywithdraw(param);
			map.put("0", "成功");
		}else{
			map.put("0", duanxin.get("0"));
		}
		

		return map;
	}

	public Map<String, Object> enterWithdraw(String servicePersonal,String type) {
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("servicePersonal", servicePersonal);
		param.put("type", type);
		String number="";
		number=auntDao.enterWithdraw(param);
		String count=auntDao.getAuntInformationByName(servicePersonal).getBalance()+"";
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("count", count);
		map.put("number", number);
		return map;
	}

	public Map<String, Object> wode(String servicePersonal) {
	ServicePersonal sp=auntDao.getAuntInformationByName(servicePersonal);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("ServicePersonal", sp);
	return map;
	}

	public Map<String, Object> grab( String servicePersonal) throws ParseException {
		Map<String, Object> map = new HashMap<String,Object>();
		ServicePersonal sp2=auntDao.findAuntByServicePersonal(servicePersonal);
		String[] types=sp2.getServiceType().split(",");
			List<AuntOrderQuery> grab2=auntDao.findPaiedOrder();
			List<AuntOrderQuery> grab=new ArrayList<AuntOrderQuery>();
			SimpleDateFormat sp=new SimpleDateFormat("yyyyMMddHHmmss");
			SimpleDateFormat shifen=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String name="";
			AuntOrderQuery oq=null;
			for(AuntOrderQuery aq:grab2){
				name=wodeDao.findUserByPhone(aq.getPhoneNumber()).getPetName();
				if(aq.getType().equals("日常保洁")){
					oq=new AuntOrderQuery(aq.getOrderNumber(),aq.getType(),aq.getOrderTime(),aq.getAddress()+aq.getDetailAddress(),name,aq.getOrderState(),aq.getTimeSlot()+"("+aq.getServiceTime()+"小时)",aq.getService_Price(),"",aq.getRealPhoneNumber(),shifen.format(sp.parse(aq.getOrderNumber())),aq.getLocation(),aq.getCleaningSupplies_Price()+"");
				}else{
					oq=new AuntOrderQuery(aq.getOrderNumber(),aq.getType(),aq.getOrderTime()+" "+aq.getTimeSlot(),aq.getAddress()+aq.getDetailAddress(),name,aq.getOrderState(),aq.getServiceTime(),aq.getService_Price(),"",aq.getRealPhoneNumber(),shifen.format(sp.parse(aq.getOrderNumber())),aq.getLocation(),"");
				}
				grab.add(oq);
			}

			//周期订单集合
			List<OrderCircle> ocs=auntDao.findPaiedCircle();
			ocs.addAll(auntDao.findNograbCircle());
			//遍历加入里面
			for(OrderCircle oc:ocs){
				name=wodeDao.findUserByPhone(oc.getPhoneNumber()).getPetName();
				String[] days=oc.getCircle().split(";");
				String[] day=days[0].split(",");
				String[] slot=days[1].split(",");
				SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd EEEE");
				SimpleDateFormat to2=new SimpleDateFormat("EEEE");
				SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
				Date date=from.parse(day[0]);
				String da=to.format(date);
				String da2=to2.format(date);
				if(days[2].equals("一周一次")){
					//					orderNumber 1				type  	2							3	orderTime 		4	address 	detailAddress 		5orderState	 6timeSlot 	7service_Price	8	9serviceTime10
					oq=new AuntOrderQuery(oc.getOrderNumber(),"周期订单(一周一次)",da,oc.getAddress()+oc.getDetailAddress(),name,oc.getOrderState(),da2+slot[0]+"("+oc.getServiceTime()+"小时)",oc.getService_price(),days[3]+"周",oc.getRealPhoneNumber(),shifen.format(sp.parse(oc.getOrderNumber())),oc.getLocation(),oc.getCleaningSupplies_Price());
						grab.add(oq);
				}else if(days[2].equals("两周一次")){
					oq=new AuntOrderQuery(oc.getOrderNumber(),"周期订单(两周一次)",da,oc.getAddress()+oc.getDetailAddress(),name,oc.getOrderState(),da2+slot[0]+"("+oc.getServiceTime()+"小时)",oc.getService_price(),Integer.valueOf(days[3])+"次",oc.getRealPhoneNumber(),shifen.format(sp.parse(oc.getOrderNumber())),oc.getLocation(),oc.getCleaningSupplies_Price());
						grab.add(oq);
				}else if(days[2].equals("一周多次")){
					String times="";
					int a=day.length/Integer.valueOf(days[3]);
					for(int x=0;x<a;x++){
						String str=to2.format(from.parse(day[x]));
						times=times+str+slot[x]+"("+getTimeCount(slot[x])+"小时);";
					}
					//						orderNumber 				type  				orderTime 			address 	detailAddress 			orderState	 	timeSlot 		service_Price		serviceTime
					oq=new AuntOrderQuery(oc.getOrderNumber(),"周期订单(一周多次)",da,oc.getAddress()+oc.getDetailAddress(),name,oc.getOrderState(),times,oc.getService_price(),days[3]+"周",oc.getRealPhoneNumber(),shifen.format(sp.parse(oc.getOrderNumber())),oc.getLocation(),oc.getCleaningSupplies_Price());
					grab.add(oq);
				}
			}
			List<AuntOrderQuery> grab1=new ArrayList<AuntOrderQuery>();
			for(AuntOrderQuery aq2:grab){
				if(sp2.getHoneLocation().equals(aq2.getLocation())){
					for(int i=0;i<types.length;i++){
						if(types[i].equals(aq2.getType())){
							grab1.add(aq2);
							
						}
					}
					
				}
			}
			List<AuntOrderQuery> grab3=new ArrayList<AuntOrderQuery>();
			grab1=paixuAuntOrderQuery(grab1);
			for(AuntOrderQuery aq3:grab1){
				if(aq3.getOrderState().equals("待接单")){
					grab3.add(aq3);
				}
			}
			for(AuntOrderQuery aq3:grab1){
				if(aq3.getOrderState().equals("已接单")){
					grab3.add(aq3);
				}
			}
			
			map.put("grab",grab3);
			map.put("state", sp2.getStage());
			return map;
		
	}
/* ===单次预约
 * 1获取订单预约的 日期 和时间段
 * 2获取阿姨所有'已接单'状态的订单
 * 3    1  判断日期相同 2没有订单的开始时间或者结束时间在时间段内
 * 		1     判断没有周期订单日期相同2没有周期的开始时间或结束时间在时间段内
 * 4返回成功  返回失败  
 * ===周期预约
 * 1转换为N个日期时间段
 * 2进行N此单次预约		
 * 
 */
	public Map<String, Object> yzGrab(String orderNumber,String type,String servicePersonal) throws ParseException{
		Map<String,Object> result=new HashMap<String,Object>();
		SimpleDateFormat from=new SimpleDateFormat("yyyy年MM月dd日");
		SimpleDateFormat to=new SimpleDateFormat("yyyy-MM-dd");
		result.put("0", "成功");
		if(type.equals("日常保洁")){
			ROrder r1=orderDao.findROrder(orderNumber);
			/* 1是周期订单 获取周期订单的circle字段
			 * 2切割成 日期 , 时间段,一周几次
			 * 3遍历日期天数  并获取同天时间段timeSlot
			 * 4add加入rlist List 
			 */
			List<DayTime> rlist=new ArrayList<DayTime>();
			//构建目标订单 日期 时间段List对象
			if(r1.getOrderTime()==null){
				String circle=r1.getCircle();
				String[] days=circle.split(";");
				String[] day=days[0].split(",");
				String[] timeSlot=days[1].split(",");
				for(int i=0;i<day.length;i++){
					rlist.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
				}
			}else{
				rlist.add(new DayTime(r1.getOrderTime(),r1.getTimeSlot()));
			}
			
			for(DayTime r:rlist){
				List<AuntOrderQuery> auntOrders=auntDao.findAuntWorkingOrder(servicePersonal);
				List<DayTime> daytime=new ArrayList<DayTime>();
				//获取已接周期订单的 每一组 day  timeSlot
				List<OrderCircle> circleOrders=auntDao.findAuntWorkingCircle(servicePersonal);
				/* 1获取每个周期订单的circle字段
				 * 2切割成 日期 , 时间段,一周几次
				 * 3遍历日期天数  并获取同天时间段timeSlot
				 * 4add加入daytime List
				 */
				for(OrderCircle oc:circleOrders){
					String circle=oc.getCircle();
					String[] days=circle.split(";");
					String[] day=days[0].split(",");
					String[] timeSlot=days[1].split(",");
					for(int i=0;i<day.length;i++){
						daytime.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
					}
				}
				for(AuntOrderQuery aq:auntOrders){
					daytime.add(new DayTime(aq.getOrderTime(),aq.getTimeSlot()));
				}
				for(DayTime aq:daytime){
					//这个订单是在想抢的订单那一天
					if(aq.getDay().equals(r.getDay())){
							String slot=aq.getTimeSlot();
							//获取想抢订单的开始 结束时间int
							int start=0;
							int end=0;
							int aqstart=0;
							int aqend=0;
							if(r.getTimeSlot().substring(0, 1).equals("8")){
								start=8;
							}else if(r.getTimeSlot().substring(0, 1).equals("9")){
								start=9;
							}else{
								start=Integer.valueOf(r.getTimeSlot().substring(0, 2));
							}
							//是日常保洁订单 存在时间段
							if(r.getTimeSlot().length()>=6){
								end=Integer.valueOf(r.getTimeSlot().substring(r.getTimeSlot().length()-5, r.getTimeSlot().length()-3));
							}else{
								end=start+4;
							}
							if(aq.getTimeSlot().substring(0, 1).equals("8")){
								aqstart=8;
							}else if(aq.getTimeSlot().substring(0, 1).equals("9")){
								aqstart=9;
							}else{
								aqstart=Integer.valueOf(aq.getTimeSlot().substring(0, 2));
							}
							if(aq.getTimeSlot().length()>=6){
								aqend=Integer.valueOf(aq.getTimeSlot().substring(aq.getTimeSlot().length()-5, aq.getTimeSlot().length()-3));
							}else{
								aqend=aqstart+4;
							}
							//判断该订单与要抢的订单是否有交集
							if((start<=aqstart&&aqstart<=end)||(start<=aqend&&aqend<=end)){
								result.put("0", "失败");
							}
					}
					
				}
			}
			
			
		}else if(type.equals("沙发护理")){
			SOrder r=orderDao.findSOrder(orderNumber);
			List<AuntOrderQuery> auntOrders=auntDao.findAuntWorkingOrder(servicePersonal);
			List<DayTime> daytime=new ArrayList<DayTime>();
			//获取已接周期订单的 每一组 day  timeSlot
			List<OrderCircle> circleOrders=auntDao.findAuntWorkingCircle(servicePersonal);
			/* 1获取每个周期订单的circle字段
			 * 2切割成 日期 , 时间段,一周几次
			 * 3遍历日期天数  并获取同天时间段timeSlot
			 * 4add加入daytime List
			 */
			for(OrderCircle oc:circleOrders){
				String circle=oc.getCircle();
				String[] days=circle.split(";");
				String[] day=days[0].split(",");
				String[] timeSlot=days[1].split(",");
				for(int i=0;i<day.length;i++){
					daytime.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
				}
			}
			for(AuntOrderQuery aq:auntOrders){
				daytime.add(new DayTime(aq.getOrderTime(),aq.getTimeSlot()));
			}
			
			
			for(DayTime aq:daytime){
				//这个订单是在想抢的订单那一天
				if(aq.getDay().equals(r.getOrderTime())){

						String slot=aq.getTimeSlot();
						//获取想抢订单的开始 结束时间int
						int start=0;
						int end=0;
						int aqstart=0;
						int aqend=0;
					
						if(r.getTimeSlot().substring(0, 1).equals("8")){
							start=8;
						}else if(r.getTimeSlot().substring(0, 1).equals("9")){
							start=9;
						}else{
							start=Integer.valueOf(r.getTimeSlot().substring(0, 2));
						}
						//是日常保洁订单 存在时间段
						if(r.getTimeSlot().length()>=6){
							end=Integer.valueOf(r.getTimeSlot().substring(r.getTimeSlot().length()-5, r.getTimeSlot().length()-3));
						}else{
							end=start+4;
						}
						if(aq.getTimeSlot().substring(0, 1).equals("8")){
							aqstart=8;
						}else if(aq.getTimeSlot().substring(0, 1).equals("9")){
							aqstart=9;
						}else{
							aqstart=Integer.valueOf(aq.getTimeSlot().substring(0, 2));
						}
						if(aq.getTimeSlot().length()>=6){
							aqend=Integer.valueOf(aq.getTimeSlot().substring(aq.getTimeSlot().length()-5, aq.getTimeSlot().length()-3));
						}else{
							aqend=aqstart+4;
						}
						//判断该订单与要抢的订单是否有交集
						if((start<=aqstart&&aqstart<=end)||(start<=aqend&&aqend<=end)){
							result.put("0", "失败");
						}
					
				}
				
			}
		}else if(type.equals("开荒保洁")){
			KOrder r=orderDao.findKOrder(orderNumber);
			List<AuntOrderQuery> auntOrders=auntDao.findAuntWorkingOrder(servicePersonal);
			List<DayTime> daytime=new ArrayList<DayTime>();
			//获取已接周期订单的 每一组 day  timeSlot
			List<OrderCircle> circleOrders=auntDao.findAuntWorkingCircle(servicePersonal);
			/* 1获取每个周期订单的circle字段
			 * 2切割成 日期 , 时间段,一周几次
			 * 3遍历日期天数  并获取同天时间段timeSlot
			 * 4add加入daytime List
			 */
			for(OrderCircle oc:circleOrders){
				String circle=oc.getCircle();
				String[] days=circle.split(";");
				String[] day=days[0].split(",");
				String[] timeSlot=days[1].split(",");
				for(int i=0;i<day.length;i++){
					daytime.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
				}
			}
			for(AuntOrderQuery aq:auntOrders){
				daytime.add(new DayTime(aq.getOrderTime(),aq.getTimeSlot()));
			}
			
			
			for(DayTime aq:daytime){
				//这个订单是在想抢的订单那一天
				if(aq.getDay().equals(r.getOrderTime())){

						String slot=aq.getTimeSlot();
						//获取想抢订单的开始 结束时间int
						int start=0;
						int end=0;
						int aqstart=0;
						int aqend=0;
					
						if(r.getTimeSlot().substring(0, 1).equals("8")){
							start=8;
						}else if(r.getTimeSlot().substring(0, 1).equals("9")){
							start=9;
						}else{
							start=Integer.valueOf(r.getTimeSlot().substring(0, 2));
						}
						//是日常保洁订单 存在时间段
						if(r.getTimeSlot().length()>=6){
							end=Integer.valueOf(r.getTimeSlot().substring(r.getTimeSlot().length()-5, r.getTimeSlot().length()-3));
						}else{
							end=start+4;
						}
						if(aq.getTimeSlot().substring(0, 1).equals("8")){
							aqstart=8;
						}else if(aq.getTimeSlot().substring(0, 1).equals("9")){
							aqstart=9;
						}else{
							aqstart=Integer.valueOf(aq.getTimeSlot().substring(0, 2));
						}
						if(aq.getTimeSlot().length()>=6){
							aqend=Integer.valueOf(aq.getTimeSlot().substring(aq.getTimeSlot().length()-5, aq.getTimeSlot().length()-3));
						}else{
							aqend=aqstart+4;
						}
						//判断该订单与要抢的订单是否有交集
						if((start<=aqstart&&aqstart<=end)||(start<=aqend&&aqend<=end)){
							result.put("0", "失败");
						}
					
				}
				
			}
		}else if(type.equals("家电清洗")){
			JOrder r=orderDao.findJOrder(orderNumber);
			List<AuntOrderQuery> auntOrders=auntDao.findAuntWorkingOrder(servicePersonal);
			List<DayTime> daytime=new ArrayList<DayTime>();
			//获取已接周期订单的 每一组 day  timeSlot
			List<OrderCircle> circleOrders=auntDao.findAuntWorkingCircle(servicePersonal);
			/* 1获取每个周期订单的circle字段
			 * 2切割成 日期 , 时间段,一周几次
			 * 3遍历日期天数  并获取同天时间段timeSlot
			 * 4add加入daytime List
			 */
			for(OrderCircle oc:circleOrders){
				String circle=oc.getCircle();
				String[] days=circle.split(";");
				String[] day=days[0].split(",");
				String[] timeSlot=days[1].split(",");
				for(int i=0;i<day.length;i++){
					daytime.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
				}
			}
			for(AuntOrderQuery aq:auntOrders){
				daytime.add(new DayTime(aq.getOrderTime(),aq.getTimeSlot()));
			}
			
			
			for(DayTime aq:daytime){
				//这个订单是在想抢的订单那一天
				if(aq.getDay().equals(r.getOrderTime())){

						String slot=aq.getTimeSlot();
						//获取想抢订单的开始 结束时间int
						int start=0;
						int end=0;
						int aqstart=0;
						int aqend=0;
					
						if(r.getTimeSlot().substring(0, 1).equals("8")){
							start=8;
						}else if(r.getTimeSlot().substring(0, 1).equals("9")){
							start=9;
						}else{
							start=Integer.valueOf(r.getTimeSlot().substring(0, 2));
						}
						//是日常保洁订单 存在时间段
						if(r.getTimeSlot().length()>=6){
							end=Integer.valueOf(r.getTimeSlot().substring(r.getTimeSlot().length()-5, r.getTimeSlot().length()-3));
						}else{
							end=start+4;
						}
						if(aq.getTimeSlot().substring(0, 1).equals("8")){
							aqstart=8;
						}else if(aq.getTimeSlot().substring(0, 1).equals("9")){
							aqstart=9;
						}else{
							aqstart=Integer.valueOf(aq.getTimeSlot().substring(0, 2));
						}
						if(aq.getTimeSlot().length()>=6){
							aqend=Integer.valueOf(aq.getTimeSlot().substring(aq.getTimeSlot().length()-5, aq.getTimeSlot().length()-3));
						}else{
							aqend=aqstart+4;
						}
						//判断该订单与要抢的订单是否有交集
						if((start<=aqstart&&aqstart<=end)||(start<=aqend&&aqend<=end)){
							result.put("0", "失败");
						}
					
				}
				
			}
		}else if(type.equals("地板护理")){
			DOrder r=orderDao.findDOrder(orderNumber);
			List<AuntOrderQuery> auntOrders=auntDao.findAuntWorkingOrder(servicePersonal);
			List<DayTime> daytime=new ArrayList<DayTime>();
			//获取已接周期订单的 每一组 day  timeSlot
			List<OrderCircle> circleOrders=auntDao.findAuntWorkingCircle(servicePersonal);
			/* 1获取每个周期订单的circle字段
			 * 2切割成 日期 , 时间段,一周几次
			 * 3遍历日期天数  并获取同天时间段timeSlot
			 * 4add加入daytime List
			 */
			for(OrderCircle oc:circleOrders){
				String circle=oc.getCircle();
				String[] days=circle.split(";");
				String[] day=days[0].split(",");
				String[] timeSlot=days[1].split(",");
				for(int i=0;i<day.length;i++){
					daytime.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
				}
			}
			for(AuntOrderQuery aq:auntOrders){
				daytime.add(new DayTime(aq.getOrderTime(),aq.getTimeSlot()));
			}
			
			
			for(DayTime aq:daytime){
				//这个订单是在想抢的订单那一天
				if(aq.getDay().equals(r.getOrderTime())){

						String slot=aq.getTimeSlot();
						//获取想抢订单的开始 结束时间int
						int start=0;
						int end=0;
						int aqstart=0;
						int aqend=0;
					
						if(r.getTimeSlot().substring(0, 1).equals("8")){
							start=8;
						}else if(r.getTimeSlot().substring(0, 1).equals("9")){
							start=9;
						}else{
							start=Integer.valueOf(r.getTimeSlot().substring(0, 2));
						}
						//是日常保洁订单 存在时间段
						if(r.getTimeSlot().length()>=6){
							end=Integer.valueOf(r.getTimeSlot().substring(r.getTimeSlot().length()-5, r.getTimeSlot().length()-3));
						}else{
							end=start+4;
						}
						if(aq.getTimeSlot().substring(0, 1).equals("8")){
							aqstart=8;
						}else if(aq.getTimeSlot().substring(0, 1).equals("9")){
							aqstart=9;
						}else{
							aqstart=Integer.valueOf(aq.getTimeSlot().substring(0, 2));
						}
						if(aq.getTimeSlot().length()>=6){
							aqend=Integer.valueOf(aq.getTimeSlot().substring(aq.getTimeSlot().length()-5, aq.getTimeSlot().length()-3));
						}else{
							aqend=aqstart+4;
						}
						//判断该订单与要抢的订单是否有交集
						if((start<=aqstart&&aqstart<=end)||(start<=aqend&&aqend<=end)){
							result.put("0", "失败");
						}
					
				}
				
			}
		}else if(type.equals("擦玻璃")){
			COrder r=orderDao.findCOrder(orderNumber);
			List<AuntOrderQuery> auntOrders=auntDao.findAuntWorkingOrder(servicePersonal);
			List<DayTime> daytime=new ArrayList<DayTime>();
			//获取已接周期订单的 每一组 day  timeSlot
			List<OrderCircle> circleOrders=auntDao.findAuntWorkingCircle(servicePersonal);
			/* 1获取每个周期订单的circle字段
			 * 2切割成 日期 , 时间段,一周几次
			 * 3遍历日期天数  并获取同天时间段timeSlot
			 * 4add加入daytime List
			 */
			for(OrderCircle oc:circleOrders){
				String circle=oc.getCircle();
				String[] days=circle.split(";");
				String[] day=days[0].split(",");
				String[] timeSlot=days[1].split(",");
				for(int i=0;i<day.length;i++){
					daytime.add(new DayTime(to.format(from.parse(day[i])),timeSlot[i]));
				}
			}
			for(AuntOrderQuery aq:auntOrders){
				daytime.add(new DayTime(aq.getOrderTime(),aq.getTimeSlot()));
			}
			
			
			for(DayTime aq:daytime){
				//这个订单是在想抢的订单那一天
				if(aq.getDay().equals(r.getOrderTime())){

						String slot=aq.getTimeSlot();
						//获取想抢订单的开始 结束时间int
						int start=0;
						int end=0;
						int aqstart=0;
						int aqend=0;
					
						if(r.getTimeSlot().substring(0, 1).equals("8")){
							start=8;
						}else if(r.getTimeSlot().substring(0, 1).equals("9")){
							start=9;
						}else{
							start=Integer.valueOf(r.getTimeSlot().substring(0, 2));
						}
						//是日常保洁订单 存在时间段
						if(r.getTimeSlot().length()>=6){
							end=Integer.valueOf(r.getTimeSlot().substring(r.getTimeSlot().length()-5, r.getTimeSlot().length()-3));
						}else{
							end=start+4;
						}
						if(aq.getTimeSlot().substring(0, 1).equals("8")){
							aqstart=8;
						}else if(aq.getTimeSlot().substring(0, 1).equals("9")){
							aqstart=9;
						}else{
							aqstart=Integer.valueOf(aq.getTimeSlot().substring(0, 2));
						}
						if(aq.getTimeSlot().length()>=6){
							aqend=Integer.valueOf(aq.getTimeSlot().substring(aq.getTimeSlot().length()-5, aq.getTimeSlot().length()-3));
						}else{
							aqend=aqstart+4;
						}
						//判断该订单与要抢的订单是否有交集
						if((start<=aqstart&&aqstart<=end)||(start<=aqend&&aqend<=end)){
							result.put("0", "失败");
						}
					
				}
				
			}}
		return result;
	}
	public  synchronized  Map<String, Object> affirmGrab(String orderNumber, String type,String servicePersonal) throws ParseException {
		
		Map<String,Object> yz=yzGrab(orderNumber,type,servicePersonal);
		if(yz.get("0").equals("失败")){
			return yz;
		}
		
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("orderNumber", orderNumber);
		param.put("servicePersonal", servicePersonal);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("0", "已被抢");
		if(type.equals("日常保洁")||type.equals("周期订单(一周多次)")||type.equals("周期订单(一周一次)")||type.equals("周期订单(两周一次)")){
			ROrder r=orderDao.findROrder(orderNumber);
			if(!r.getOrderState().equals("待接单")){
				return map;
			}
			auntDao.raffirm(param);
			//发送短信
			new YunTongXun().register(r.getPhoneNumber(),"86577",null);
		}else if(type.equals("沙发护理")){
			SOrder r=orderDao.findSOrder(orderNumber);
			if(!r.getOrderState().equals("待接单")){
				return map;
			}
			auntDao.saffirm(param);
			//发送短信
			new YunTongXun().register(r.getPhoneNumber(),"86577",null);
		}else if(type.equals("开荒保洁")){
			KOrder r=orderDao.findKOrder(orderNumber);
			if(!r.getOrderState().equals("待接单")){
				return map;
			}
			auntDao.kaffirm(param);
			//发送短信
			new YunTongXun().register(r.getPhoneNumber(),"86577",null);
		}else if(type.equals("家电清洗")){
			JOrder r=orderDao.findJOrder(orderNumber);
			if(!r.getOrderState().equals("待接单")){
				return map;
			}
			auntDao.jaffirm(param);
			//发送短信
			new YunTongXun().register(r.getPhoneNumber(),"86577",null);
		}else if(type.equals("地板护理")){
			DOrder r=orderDao.findDOrder(orderNumber);
			if(!r.getOrderState().equals("待接单")){
				return map;
			}
			auntDao.daffirm(param);
			//发送短信
			new YunTongXun().register(r.getPhoneNumber(),"86577",null);
		}else if(type.equals("擦玻璃")){
			COrder r=orderDao.findCOrder(orderNumber);
			if(!r.getOrderState().equals("待接单")){
				return map;
			}
			auntDao.caffirm(param);
			//发送短信
			new YunTongXun().register(r.getPhoneNumber(),"86577",null);
		}
		map.put("0", "成功");
		
		return map;
	}

	//阿姨反馈
	public Map<String, Object> addFeedBack(String phoneNumber, String text) {
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("phoneNumber",phoneNumber);
		param.put("text",text);
		Map<String, Object> map = new HashMap<String,Object>();
		if(text==null){
			map.put("0","失败");
			return map;
		}
		auntDao.addFeedBack(param);
		map.put("0", "成功");
		return map;
	}

	public Map<String, Object> login(String servicePersonal, String pwd) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(servicePersonal==null||pwd==null||servicePersonal.equals("")||pwd.equals("")){
			map.put("0", "账号密码错误");
			return map;
		}
		Map<String,Object> param=new HashMap<String,Object>();
		param.put("servicePersonal", servicePersonal);
		param.put("pwd", pwd);
		ServicePersonal sp=auntDao.auntLogin(param);
		if(sp==null){
			map.put("0", "账号密码错误");
			return map;
		}
		map.put("0", "成功");
		return map;
	}

	public Map<String, Object> clickNotified(String id) {
		Map<String,Object> map=new HashMap<String,Object>();

		Notified notified=auntDao.findNotifiedById( id);
		if(notified.getState().equals("未读")){
			auntDao.updateNotifiedById(id);
			auntDao.updateAuntNotifiedById(notified.getPhoneNumber());
			map.put("0", "成功");
		}else{
			map.put("0", "失败");
		}
		return map;
	}
	public static List paixuAuntOrderQuery(List<AuntOrderQuery> listall){
		for (int i = 0; i < listall.size(); i++) {   
	        int k = i;   
	        for (int j = listall.size() - 1; j >i; j--)  {   
	            if (Long.valueOf(listall.get(j).getOrderNumber()) > Long.valueOf(listall.get(k).getOrderNumber()))  k = j;   
	        }   
	        AuntOrderQuery  temp = listall.get(i);   
	        listall.set(i,listall.get(k));   
	        listall.set(k,temp);   
	    }   
		return listall;
		
	}
	public static List paixuEvaluation(List<Evaluation> listall) throws ParseException{
		for (int i = 0; i < listall.size(); i++) {   
	        int k = i;   
	        for (int j = listall.size() - 1; j >i; j--)  { 
	            if ((new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(listall.get(j).getEvaluationTime()).getTime())> (new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(listall.get(k).getEvaluationTime()).getTime()))  k = j;   
	        }   
	        Evaluation  temp = listall.get(i);   
	        listall.set(i,listall.get(k));   
	        listall.set(k,temp);   
	    }   
		return listall;
		
	}
	public static List paixuIncome(List<Income> listall) throws ParseException{
		for (int i = 0; i < listall.size(); i++) {   
	        int k = i;   
	        for (int j = listall.size() - 1; j >i; j--)  { 
	            if ((new SimpleDateFormat("yyyy-MM-dd").parse(listall.get(j).getTime()).getTime())> (new SimpleDateFormat("yyyy-MM-dd").parse(listall.get(k).getTime()).getTime()))  k = j;   
	        }   
	        Income  temp = listall.get(i);   
	        listall.set(i,listall.get(k));   
	        listall.set(k,temp);   
	    }   
		return listall;
		
	}
	public static List paixuNotified(List<Notified> listall) throws ParseException{
		for (int i = 0; i < listall.size(); i++) {   
	        int k = i;   
	        for (int j = listall.size() - 1; j >i; j--)  { 
	            if ((new SimpleDateFormat("yyyy-MM-dd").parse(listall.get(j).getNotifiedTime()).getTime())> (new SimpleDateFormat("yyyy-MM-dd").parse(listall.get(k).getNotifiedTime()).getTime()))  k = j;   
	        }   
	        Notified  temp = listall.get(i);   
	        listall.set(i,listall.get(k));   
	        listall.set(k,temp);   
	    }   
		return listall;
		
	}
	public static List paixuWithdrawDetail(List<WithdrawDetail> listall) throws ParseException{
		for (int i = 0; i < listall.size(); i++) {   
	        int k = i;   
	        for (int j = listall.size() - 1; j >i; j--)  { 
	            if ((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(listall.get(j).getTime()).getTime())> (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(listall.get(k).getTime()).getTime()))  k = j;   
	        }   
	        WithdrawDetail  temp = listall.get(i);   
	        listall.set(i,listall.get(k));   
	        listall.set(k,temp);   
	    }   
		return listall;
		
	}
	public  void stateNotified(String phoneNumber,String type,String orderNumber) throws ParseException, APIConnectionException, APIRequestException{
		//发送通知
			Map<String,Object> param=new HashMap<String,Object>();
			param.put("type", "用户");
			param.put("detailType", "订单通知");
			param.put("notifiedContent", "您的["+type+orderNumber+"]订单现已经完成!");
			param.put("notifiedTime",new Date());
			param.put("phoneNumber", phoneNumber);

			auntDao.createNotifiedMessage(param);
			dao.updateNotifiedUser(phoneNumber);
			ControllService.pushOne("8f6965a441a0aa10011c7063","1dd0e3924e7394137bc20d57",param.get("notifiedContent")+"",phoneNumber);

	}
	//根据slot(8:00-11:00)计算是几小时
	private int getTimeCount(String slot){
		String[] str=slot.split(":");
		return Integer.valueOf(str[1].substring(str[1].length()-2, str[1].length()))-Integer.valueOf(str[0]);
	}
	
}	
	
	
	
	
	

	
	 
	
	
	

